#pragma once

#include <jsoncpp/json/json.h>
#include <vector>
#include "../repository/learn-repository.hh"
#include "../poco/word-status.hh"

namespace WordMemory
{
    class LearnService
    {
    public:
        static LearnService &instance()
        {
            static LearnService inst;
            return inst;
        }

        Json::Value getLearningStats(int user_id)
        {
            try
            {
                return LearnRepository::instance().getLearningStats(user_id);
            }
            catch (const RepositoryExcept& e)
            {
                spdlog::error("LearnService: Error getting learning stats for user_id {}: {}", user_id, e.what());
                throw ServiceExcept("Failed to retrieve learning statistics: " + std::string(e.what()));
            }
        }

        Json::Value getDailyLearningSummary(int user_id, int days = 30)
        {
            try
            {
                std::vector<DailyLearningStat> summary_data = LearnRepository::instance().getDailyLearningSummary(user_id, days);
                Json::Value json_array(Json::arrayValue);
                for (const auto& stat : summary_data) {
                    json_array.append(stat.to_json());
                }
                return json_array;
            }
            catch (const RepositoryExcept& e)
            {
                spdlog::error("LearnService: Error getting daily learning summary for user_id {}: {}", user_id, e.what());
                throw ServiceExcept("Failed to retrieve daily learning summary: " + std::string(e.what()));
            }
        }


    private:
        LearnService() = default;
        LearnService(const LearnService &) = delete;
        LearnService &operator=(const LearnService &) = delete;
    };
}